package com.ms.content.controller;

import java.util.Arrays;
import java.util.Map;
import com.ms.content.vo.MaVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.ms.content.entity.MiaoshaActivityEntity;
import com.ms.content.service.MiaoshaActivityService;
import com.ms.common.utils.PageUtils;
import com.ms.common.utils.R;



/**
 * 这个是秒杀活动表
 *
 * @author wc
 * @email sunlightcs@gmail.com
 * @date 2022-02-22 11:56:10
 *
 * */
@RestController
@RequestMapping("content/miaoshaactivity")
public class MiaoshaActivityController {
    @Autowired
    private MiaoshaActivityService miaoshaActivityService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = miaoshaActivityService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		MiaoshaActivityEntity miaoshaActivity = miaoshaActivityService.getById(id);

        return R.ok().put("miaoshaActivity", miaoshaActivity);
    }


    /**
     * 获取临时地址隐藏
     */
    @RequestMapping("/getPath")
    public R info(@RequestParam("msid") Integer msid,@RequestParam("uid") String uid){
        //生成秒杀地址
        String path  =miaoshaActivityService.createMiaoshaPath(msid,uid);
        System.out.println("访问秒杀地址"+path);
        if (StringUtils.isEmpty(path)){
            return R.error("获取地址已达上限");
        }
        return R.ok().put("path",path);
    }
    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody MaVo maVo){
		miaoshaActivityService.inssave(maVo);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody MiaoshaActivityEntity miaoshaActivity){
		miaoshaActivityService.updateById(miaoshaActivity);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
		miaoshaActivityService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }

    /**
     * 修改  flag中的 活动状态
     */
    @RequestMapping("/upflag/{id}")
    public R upflag(@PathVariable("id") Integer id){
		miaoshaActivityService.upflag(id);
        return R.ok();
    }

}
